/*
* SMILParser
*
* Version 1.0
*
* Programmers: Jesse Louderback, Noah Pham, Chad Carpenter, and Marc Hindman
*/
package com.SmilDial;

import java.util.ArrayList;

import CloudPackage.Cloud;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.View;
import android.widget.*;

import com.SmilDial.ImageAdapter;

public class ImageMenu extends Activity {
	Button btnNext;
	Button btnCancel;
	Bundle SMIL_Bundle;
	EditText inputImgStartTime;
	EditText inputImgDurationTime;
	
	/**
	 *  Called when the activity is first created. 
	 */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.image_menu);       
        
        inputImgStartTime = (EditText)findViewById(R.id.inputStartTime);
        inputImgDurationTime = (EditText)findViewById(R.id.inputDurationTime);
        btnNext = (Button)findViewById(R.id.btnNext);
        btnCancel = (Button)findViewById(R.id.btnCancel);
        
        //Gets the bundle from last intent, TextMenu.java
        SMIL_Bundle = getIntent().getExtras();
        
        final Gallery myGallery = (Gallery)findViewById(R.id.imgGallery);
        Cloud smilDialCloud = SmilDialActivity.getCloudInstance();
        ArrayList<String> cloudImages = smilDialCloud.getListOfImages();
        final String[] listOfImages = cloudImages.toArray(new String[cloudImages.size()]); 
        
        myGallery.setAdapter(new ImageAdapter(this, listOfImages));
        
        btnNext.setOnClickListener(new View.OnClickListener() {
			
			public void onClick(View v) {
				Intent intent = new Intent(ImageMenu.this, LoadingScreenSoundMenu.class);				
				
				String selectedImage = listOfImages[(int) myGallery.getSelectedItemId()].toString();
				Float imgStartTime = (float) 0.0;
				Float imgDurationTime = (float) 0.0;
				Boolean inputGood = true;
				
				if (inputImgStartTime.getText().toString().length() >= 1) {
					inputGood = numValidationInput(inputImgStartTime.getText().toString());
					imgStartTime = new Float(inputImgStartTime.getText().toString());
				}
				
				if (inputImgDurationTime.getText().toString().length() >= 1) {
					inputGood = numValidationInput(inputImgDurationTime.getText().toString());
					imgDurationTime = new Float(inputImgDurationTime.getText().toString());
				}

				if (inputGood) {
					SMIL_Bundle.putString("imgImageURL", selectedImage);
					SMIL_Bundle.putFloat("imgStartTime", imgStartTime);
					SMIL_Bundle.putFloat("imgDurationTime", imgDurationTime);
					
					intent.putExtras(SMIL_Bundle);
					startActivity(intent);
				}
			}
		});
             
        btnCancel.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				startActivity(new Intent(ImageMenu.this, SmilDialActivity.class));
			}
		});
    }
    
    private Boolean numValidationInput(String inputValue) {
    	try {
    		int x = Integer.parseInt(inputValue);
    		
    		return true;
    	}
    	catch(Exception e) {
    		Toast toast=Toast.makeText(this, "Duration time must be a number.", 2000); 
    		toast.show(); 
    	}
    	
    	return false;
    }
}
